package com.pearadmin.modules.sys.controller.work;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.constant.ControllerConstant;
import com.pearadmin.common.tools.SecurityUtil;
import com.pearadmin.common.tools.SequenceUtil;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.*;
import com.pearadmin.modules.sys.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 清理工作记录Controller
 *
 * @author Mr.Ling
 * @date 2021-11-26
 */
@RestController
@Api(tags = {"清理工作记录"})
@RequestMapping(ControllerConstant.API_WORK_PREFIX + "clean")
public class WorkCleanController extends BaseController {
    private String prefix = "work/clean" ;

    @Autowired
    private IWorkCleanService workCleanService;

    @Autowired
    private IAnodeCodeService anodeCodeService;

    @Autowired
    private ISlotInfoService slotInfoService;

    @Autowired
    private IPoleInfoService poleInfoService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/work/clean/main','work:clean:main')")
    public ModelAndView main(Model model) {
        //model.addAttribute("anodeCodes", anodeCodeService.selectAnodeCodeList(null));
        //model.addAttribute("slotNos", slotInfoService.selectSlotInfoList(null));
        //model.addAttribute("poleNos", poleInfoService.selectPoleInfoList(null));
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询清理工作记录列表
     */
    @ResponseBody
    @ApiOperation("列表 清理工作")
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/work/clean/data','work:clean:data')")
    public ResultTable list(@ModelAttribute WorkClean workClean, PageDomain pageDomain) {
        PageInfo<WorkClean> pageInfo = workCleanService.selectWorkCleanPage(workClean, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 新增清理工作记录
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/work/clean/add','work:clean:add')")
    public ModelAndView add(Model model) {
        //model.addAttribute("slotNos", slotInfoService.selectSlotInfoList(null));
        //model.addAttribute("poleNos", poleInfoService.selectPoleInfoById(null));
        List<SysDept> list = sysDeptService.list(null);
        model.addAttribute("depts", list);
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存清理工作记录
     */
    @ResponseBody
    @ApiOperation("保存 清理工作")
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/work/clean/add','work:clean:add')")
    public Result save(@RequestBody WorkClean workClean) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workClean.setCreateTime(LocalDateTime.now());
        workClean.setCreateBy(sysUser.getUserId());
        workClean.setCreateName(sysUser.getRealName());
        //cleanByPoleAndSlot(workClean);
        workClean.setId(SequenceUtil.makeStringId());
        return decide(workCleanService.insertWorkClean(workClean));
    }

    //private void cleanByPoleAndSlot(@RequestBody WorkClean workClean) {
    //    if (workClean.getPoleNo() != null) {
    //        PoleInfo poleInfo = poleInfoService.selectPoleInfoById(workClean.getPoleNo());
    //        workClean.setPoleNo(poleInfo.getPoleNo());
    //    }
    //    if (workClean.getSlotNo() != null) {
    //        SlotInfo slotInfo = slotInfoService.selectSlotInfoById(workClean.getSlotNo());
    //        workClean.setSlotNo(slotInfo.getSlotNo());
    //    }
    //}

    /**
     * 修改清理工作记录
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/work/clean/edit','work:clean:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        WorkClean workClean = workCleanService.selectWorkCleanById(id);
        workClean.setUpdateTime(LocalDateTime.now());
        mmap.put("workClean", workClean);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存清理工作记录
     */
    @ResponseBody
    @ApiOperation("更新 清理工作")
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/work/clean/edit','work:clean:edit')")
    public Result update(@RequestBody WorkClean workClean) {
        SysUser sysUser = (SysUser) SecurityUtil.currentUserObj();
        workClean.setUpdateTime(LocalDateTime.now());
        workClean.setUpdateBy(sysUser.getUserId());
        workClean.setUpdateName(sysUser.getUsername());
        return decide(workCleanService.updateWorkClean(workClean));
    }

    /**
     * 删除清理工作记录
     */
    @ResponseBody
    @ApiOperation("批量删除 清理工作")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/work/clean/remove','work:clean:remove')")
    public Result batchRemove(String ids) {
        return decide(workCleanService.deleteWorkCleanByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @ApiOperation("根据id删除 清理工作")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/work/clean/remove','work:clean:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(workCleanService.deleteWorkCleanById(id));
    }
}
